package net.oschina.git.haxteam.zrank.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserInfoManager {

	/**
	 * 获取用户的用户名
	 * @param playerName 用户名，将会被转化为小写
	 * @return 返回XP
	 */

	public static String getUserXp(String playername){
		playername = playername.toLowerCase();
		String Xp = null;
		ConnectionManager mysqldatabase = ConnectionManager.getInstance();
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			connection = mysqldatabase.getHikariDataSource().getConnection();
			ps = connection.prepareStatement("select Xp from " + "zrank" + " where `PlayerName`=?;");
			ps.setString(1, playername);
			rs = ps.executeQuery();
			if (rs.next()){
				Xp = rs.getString("Xp");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO

				e.printStackTrace();
			}
		}
		return Xp;
	}

	/**
	 * 获取用户的用户名
	 * @param playerName 用户名，将会被转化为小写
	 * @return 返回level
	 */

	public static String getUserLevel(String playername){
		playername = playername.toLowerCase();
		String level = null;
		ConnectionManager mysqldatabase = ConnectionManager.getInstance();
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			connection = mysqldatabase.getHikariDataSource().getConnection();
			ps = connection.prepareStatement("select Level from " + "zrank" + " where `PlayerName`=?;");
			ps.setString(1, playername);
			rs = ps.executeQuery();
			if (rs.next()){
				level = rs.getString("Level");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO

				e.printStackTrace();
			}
		}
		return level;
	}

	/**
	 * 设置用户的Xp
	 * @param username 用户名，将会被转化为小写
	 * @param xp xp
	 */

	@SuppressWarnings("resource")
	public static void setUserXp(String playername, int xp){
		playername = playername.toLowerCase();
		String sxp = String.valueOf(xp);
		ConnectionManager mysqldatabase = ConnectionManager.getInstance();
		Connection connection = null;
		PreparedStatement ps = null;
		try {
			connection = mysqldatabase.getHikariDataSource().getConnection();
			ps = connection.prepareStatement("select UserName from " + "zrank" + " where `PlayerName`=?;");
			ps.setString(1, playername);
			ResultSet rs = ps.executeQuery();
			if (rs.next()){
				ps = connection.prepareStatement("update " + "zrank" + " set `Xp`=? where `PlayerName`=?;");
				ps.setString(1, sxp);
				ps.setString(2, playername);
				ps.executeUpdate();
			}else{
				ps = connection.prepareStatement("insert into " + "zrank" + " (`PlayerName`, `Xp`) values (?,?);");
				ps.setString(1, playername);
				ps.setString(2, sxp);
				ps.execute();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO

				e.printStackTrace();
			}
		}
	}

	/**
	 * 设置用户的Level
	 * @param username 用户名，将会被转化为小写
	 * @param xp xp
	 */

	@SuppressWarnings("resource")
	public static void setUserlevel(String playername, int level){
		playername = playername.toLowerCase();
		String slevel = String.valueOf(level);
		ConnectionManager mysqldatabase = ConnectionManager.getInstance();
		Connection connection = null;
		PreparedStatement ps = null;
		try {
			connection = mysqldatabase.getHikariDataSource().getConnection();
			ps = connection.prepareStatement("select UserName from " + "zrank" + " where `PlayerName`=?;");
			ps.setString(1, playername);
			ResultSet rs = ps.executeQuery();
			if (rs.next()){
				ps = connection.prepareStatement("update " + "zrank" + " set `Level`=? where `PlayerName`=?;");
				ps.setString(1, slevel);
				ps.setString(2, playername);
				ps.executeUpdate();
			}else{
				ps = connection.prepareStatement("insert into " + "zrank" + " (`PlayerName`, `Level`) values (?,?);");
				ps.setString(1, playername);
				ps.setString(2, slevel);
				ps.execute();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO

				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取用户的用户名
	 * @param playerName 用户名，将会被转化为小写
	 * @return 返回是否存在用户名
	 */

	public static boolean getCheckUserName(String UserName){
		UserName = UserName.toLowerCase();
		ConnectionManager mysqldatabase = ConnectionManager.getInstance();
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			connection = mysqldatabase.getHikariDataSource().getConnection();
			ps = connection.prepareStatement("select UserName from " + "manageacc");
			rs = ps.executeQuery("select UserName from " + "manageacc");
			int col = 0;
			col = rs.getMetaData().getColumnCount();
			while(rs.next()){
				for(int i = 1;i <= col;i++){
					if(rs.getString(i).equals(UserName)){
						return false;
					}
				}
			}

		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO

				e.printStackTrace();
			}
		}


		return true;
	}
}
